package com.product.system.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.product.system.domain.FactoryUser;
import com.product.system.domain.request.SysRoleSerch;
import com.product.system.domain.response.SysRole;
import com.product.system.domain.response.SysRoleMenu;
import com.product.system.domain.response.SysShowForm;
import com.product.system.mapper.SysRoleMapper;
import com.product.system.mapper.SysRoleMenuMapper;
import com.product.system.service.SysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SysRoleServiceImpl implements SysRoleService {


    @Resource
    SysRoleMapper sysRoleMapper;


    @Resource
    SysRoleMenuMapper sysRoleMenuMapper;







    @Override
    public PageInfo<SysRole> roleList(SysRoleSerch sysRoleSerch) {
        PageHelper.startPage(sysRoleSerch.getPageNum(),sysRoleSerch.getPageSize());

        List<SysRole> sysRoles = sysRoleMapper.roleList(sysRoleSerch);

        PageInfo<SysRole> rolePageInfo = new PageInfo<>(sysRoles);

        return rolePageInfo;
    }

    @Override
    public void roleAdd(SysRole sysRole) {

        //添加角色
        sysRoleMapper.roleAdd(sysRole);

       SysRole one = sysRoleMapper.getRoleIdByRoleName(sysRole.getRoleName());
        System.out.println(one.getRoleId()+"-----------------------roleId-------------------------------");
        log.info(one.getRoleId()+"000000000000000000000000000");


        //添加菜单权限
        if (one != null) {
            Long[] menuIds = sysRole.getMenuIds();
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            for (Long menuId : menuIds) {
                if (menuId == null) {
                    break;
                }
                sysRoleMenu.setRoleId(one.getRoleId());
                sysRoleMenu.setMenuId(menuId);
                sysRoleMenuMapper.addRoleMenu(sysRoleMenu);
            }
        }

    }

    @Override
    public Boolean roleUpdata(SysRole sysRole) {
        return sysRoleMapper.roleUpdata(sysRole);
    }

    @Override
    public List<SysRole> excelList() {
        return sysRoleMapper.excelList();
    }

    @Override
    public Boolean roleDel(Integer roleId) {
        return sysRoleMapper.roleDel(roleId);
    }

    @Override
    public List<SysShowForm> showForm() {
        return sysRoleMapper.showForm();
    }

    @Override
    public void loadinRoleAdd(SysRole sysRole) {
        sysRoleMapper.roleAdd(sysRole);
    }

    @Override
    public void loadinRoleMatchAdd(List<SysRole> cachedSysRoleList) {
        sysRoleMapper.loadinRoleMatchAdd(cachedSysRoleList);
    }

    @Override
    public FactoryUser selectSysRole(String name) {
        return sysRoleMapper.selectSysRole(name);
    }


}
